//--------------------------------------------------------------------
//     此代码由FastEasy框架自动生成
//	   生成时间 2023-09-30 01:25:11
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using FastEasy.Common.Mail;
using FastEasy.Model.RequestDTO;
using Microsoft.AspNetCore.Authorization;

namespace FastEasyAPI.Controllers
{
    /// <summary>
    /// AutoCode控制器接口
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class AutoCodeController : ControllerBase
    {
        private readonly IAutoCodeService service;

        public AutoCodeController(IAutoCodeService _service)
        {
            service = _service;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="user">邮箱或者手机号</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [FastEasyRoute(SwaggerVersion.FastEasy, "CreateCode")]
        public async Task<ResultDto> CreateCode(string user)
        {
            var result = new ResultDto();
            var code = RandomCode.CreateRandomCode();

            if (RegexMatch.IsPhone(user))
            {
                result.State = false;
                result.Message = "暂不支持手机号注册";
            }
            else if (RegexMatch.IsEmail(user))
            {
                var mime = new Mime
                {
                    Body = new MimeKit.TextPart()
                    {
                        Text = code
                    },
                    Subject = "验证码",
                    To = new MimeKit.MailboxAddress(user, user),
                };
                var autocode = new AutoCode
                {
                    Code = code,
                    CreateDate = DateTime.Now,
                    Email = user,
                    ExpDate = DateTimeOffset.Now.AddMinutes(10).ToUnixTimeSeconds(),
                    UpdateDate = DateTime.Now
                };
                if (await EmailKit.SendMail(mime) && await service.CreateEntityAsync(autocode))
                {
                    result.State = true;
                    result.Message = "发送成功请查收";
                }
                else
                {
                    result.State = false;
                    result.Message = "发送失败";
                }
            }
            else
            {
                result.State = false;
                result.Message = "请输入正确的注册号码";
            };
            return result;
        }

        /// <summary>
        /// 验证Code是否存在
        /// </summary>
        /// <param name="signDto"></param>
        /// <returns></returns>
        [HttpPost]
        [FastEasyRoute(SwaggerVersion.FastEasy, "VerifyCode")]
        public async Task<ResultDto> VerifyCode([FromBody] SignDto signDto)
        {
            ResultDto result = new() { State = false, Message = "验证码不存在" };
            var entitys = await service.GetEntitysByWhereAsync(a => a.Email == signDto.Name && a.Code == signDto.Code);
            if (entitys.Count > 0)
            {
                foreach (var item in entitys)
                {
                    if (DateTimeOffset.Now.ToUnixTimeSeconds() < item.ExpDate)
                    {
                        result.State = true;
                        result.Message = string.Empty;
                        break;
                    }
                    else
                    {
                        result.Message = "验证码已过期";
                    }
                }
            }
            return result;
        }
    }
}